﻿@model IEnumerable<Wicresoft.UnifyShow.Business.BusinessSystemModel>
@{
    ViewBag.Title = "维护业务系统";
    var types = new Wicresoft.UnifyShow2.Portal.ViewModels.DeviceAndTypes().Types;
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
    Dictionary<decimal, string> idAndSiteNames = (Dictionary<decimal, string>)ViewData["SiteIDAndName"];
    List<decimal> listSiteIDs = Model.Select(o => o.SiteID).Distinct().OrderBy(o => o).ToList();
}
@section ScriptAndCss{
    <link href="../../Content/jquery.treeview.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/css/bsadmin.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery.cookie.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.treeview.js" type="text/javascript"></script>
    <script src="../../Scripts/ALL.js" type="text/javascript"></script>
    <script type="text/javascript">
        function  initTheEditor(){
            $("#bsSave","#content").click(function()
            {
                $.post("@Url.Action("Edit", "AdminBS")",
                    $("#bsEditForm").serialize() + "&Name=" + $("#Alias", "#bsEditForm").val(),//+"&SiteId="+$("#bsSiteID","#bsEditForm").val(),
                    function () {
                        alert("更新成功");
                        var url = "@Url.Action("Tree", "AdminBSMaintain")";
                        $("#tree").load(url, function () {
                            treeView();
                        });
                    });
            });
            $("#bsPublish","#content").click(function()
            {
                $.post("@Url.Action("Publish", "AdminBS")",
                    "id=" + $("#bsID", "#bsEditForm").val(),
                    function () {
                        alert("发布成功");
                    });
            });
            $("#bsDelete","#content").click(function()
            {
                var deviceCount = $("tbody", "#gridDeviceTable").children().length;
                if (deviceCount > 0)
                {
                    alert("此业务系统有关联的设备，请删除设备后，再删除此业务系统。");
                    return false;
                }
                var isContinue = confirm("你确定要删除此业务系统？");
                if (isContinue)
                {
                    $.post("@Url.Action("Delete", "AdminBS")",
                        "id=" + $("#bsID", "#bsEditForm").val(),
                        function () {
                            alert("删除成功");
                            var url = "@Url.Action("Tree", "AdminBSMaintain")";
                            $("#tree").load(url, function () {
                                treeView();
                            });
                        });
                }
                treeView();
            });
        }

           
        function initTheDeviceAcitons()
        {
            PreventAAction("devices");
            $(".editDevice", "#devices").click(function (e)
            {
                var id = $(this).attr("data-id");
                var url = "@Url.Action("Edit", "AdminDevice")" + "?deviceID=" + id;
                $("#deviceEditDialog").dialog({
                    href: url
                });
                $("#deviceEditDialog").dialog("open");
                e.preventDefault();
            });
            $("#createDevice", "#devices").click(function (e)
            {
                var url = "@Url.Action("Add", "AdminDevice")";
                $("#deviceAddDialog").dialog({
                    href: url
                });
                $("#deviceAddDialog").dialog("open");
                e.preventDefault();
            });
            $(".deleteDevice", "#devices").click(function (e)
            {
                var isContinue = confirm("你确定要删除这个设备");
                if (isContinue)
                {
                    $.post("@Url.Action("Delete", "AdminDevice")",
                        { id: $(this).attr("data-id") },
                        function (){
                            $("#devices").load($("#devices").attr("data-url"),
                                function () {
                                    initTheDevice();
                                });
                            alert("删除成功");
                        });
                }
                e.preventDefault();
            });
        }

        function initTheDevice() {
            var bsID = $("#bsID", "#bsEditForm").val();
            var identify = $("#searchIdentify").val();
            var type = $("#searchType").val();
            var url = "@Url.Action("List", "AdminDevice")";
            url = encodeURI(url + "?bsID=" + bsID + "&identify=" + identify + "&type=" + type);
            $("#devices").attr("data-url", url);
            $("#devices").load(url, function () {
                initTheDeviceAcitons();
            });
        }
        function initTheNOBSIDDevice() {
            PreventAAction("deviceNOBSIDTable");
            $(".submitNOBSIDDevice", "#deviceNOBSIDTable").click(function (e) {
                var isContinue = confirm("你确定将此设备相关到本业务系统");
                if (isContinue) {
                    $.post("@Url.Action("SubmitNOBSIDDevice", "AdminDevice")",
                     "&SiteId=" + $("#bsSiteID", "#bsEditForm").val() + "&BusinessSystemId=" + $("#bsID", "#bsEditForm").val() + "&deviceID=" + $(this).attr("data-id") + "&identify=" + $(this).attr("data-identify"),
                           function () {
                               $("#devices").load($("#devices").attr("data-url"), function () {
                                   initTheDevice();
                               });
                               $("#devicesNOBSID").load($("#devicesNOBSID").attr("data-url"), function () {
                                   initTheNOBSIDDevice();
                               });
                           });
                }
                e.preventDefault();
            });
        }

        function treeView() {
            $("#bsTree").treeview({
                control: "#bsTreeControl",
                persist: "cookie",
                cookieId: "treeview-black_BusinessSystemTreeMaintaince",
                collapsed: true,
                unique: true
            });

            $(".node").click(function (e) {
                var id = $(this).attr("data-id");
                var url = "@Url.Action("Edit", "AdminBS")" + "?bsID=" + id;
                $("#content").load(url, function () {
                    initTheEditor();
                });
                var url2 = encodeURI("@Url.Action("List", "AdminDevice")" + "?bsID=" + id);
                $("#devices").load(url2, function () {
                    initTheDevice();
                });
                $("#devices").attr("data-url", url2);
                var url3 = "@Url.Action("NoBSIDList", "AdminDevice")" + "?bsID=" + id;
                $("#devicesNOBSID").load(url3, function () {
                    initTheNOBSIDDevice();
                });
                $("#devicesNOBSID").attr("data-url", url3);

                $("#systemID").val(id);
                e.preventDefault();
                
            });
            $("#content").empty();
            $("#devices").empty();
            $("#devicesNOBSID").empty();
        }
        $(function () {
            treeView();
            $("#searchDevice").click(function () {
                initTheDevice();
            });
            $("#searchClearDevice").click(function () {
                $("#searchIdentify").val("");
                $("#searchType")[0].selectedIndex = 0;
                initTheDevice();
            });
            $("#deviceAddDialog").dialog({
                closed: true, width: 350, height: 260, modal: true,
                buttons: [{
                    text: '保存',
                    iconCls: 'icon-ok',
                    handler: function () {
                        if ($("#Device_Identify", "#deviceAddForm").val() == "") {
                            alert("标识符不能为空！")
                        }
                        else {
                            $.post("@Url.Action("Add", "AdminDevice")",
                                $("#deviceAddForm").serialize() + "&Device.SiteId=" + $("#bsSiteID", "#bsEditForm").val() + "&Device.BusinessSystemId=" + $("#bsID", "#bsEditForm").val(),
                                function () {
                                    $("#deviceAddDialog").dialog("close");
                                    $("#devices").load($("#devices").attr("data-url"), function () {
                                        initTheDevice();
                                    });
                                });
                        }
                    }
                }, {
                    text: '取消',
                    iconCls: 'icon-cancel',
                    handler: function () { $("#deviceAddDialog").dialog("close"); }
                }]
            });
            $("#deviceEditDialog").dialog({
                closed: true, width: 350, height: 260, modal: true,
                buttons: [{
                    text: '保存',
                    iconCls: 'icon-ok',
                    handler: function () {
                        $.post("@Url.Action("Edit", "AdminDevice")",
                            $("#deviceEditForm").serialize(),
                            function () {
                                $("#deviceEditDialog").dialog("close");
                                $("#devices").load($("#devices").attr("data-url"), function () {
                                    initTheDevice();
                                });
                            });
                    }
                }, {
                    text: '取消',
                    iconCls: 'icon-cancel',
                    handler: function () { $("#deviceEditDialog").dialog("close"); }
                }]
            });
        });
    </script>
}
<div class="index_lr">
    <input type="hidden" id="systemID" />
    <ul>
        <li class="index_lr4_L">
            <div id="tree">
                @Html.Action("Tree")
            </div>
        </li>
        <li class="index_lr4_R">
            <ul>
                <li class="index_lr4_R">
                    <div class="index_lrA_title ui-icons">
                        <h3>
                            业务系统：</h3>
                    </div>
                    <div id="content">
                    </div>
                </li>
                <li class="index_lr4_R">
                    <div class="index_lrA_title ui-icons">
                        <h3>
                            此业务系统的设备列表：</h3>
                    </div>
                    <div class="index_pt_bg">
                        <table>
                            <tbody>
                                <tr>
                                    <td width="220">
                                        <label>
                                            特性值：</label>
                                        <input type="text" id="searchIdentify" name="searchIdentify" />
                                    </td>
                                    <td width="220">
                                        <label>
                                            分类：</label>
                                        <select name="searchType" id="searchType">
                                            <option value=""></option>
                                            @foreach (string item in types)
                                            {
                                                <option value="@item">@item</option>
                                            }
                                        </select>
                                    </td>
                                    <td width="220">
                                        <input type="button" id="searchDevice" value="查询" />
                                        <input type="button" id="searchClearDevice" value="重置" />
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <div id="devices" data-url="">
                        </div>
                    </div>
                </li>
                <li class="index_lr4_R">
                    <div class="index_lrA_title ui-icons">
                        <h3>
                            未相关业务系统的设备列表：</h3>
                    </div>
                    <div class="index_pt_bg">
                        <div id="devicesNOBSID" data-url="">
                        </div>
                    </div>
                </li>
            </ul>
</div>
<div id="deviceEditDialog" title="编辑设备">
</div>
<div id="deviceAddDialog" title="添加设备">
</div>
<div class="clear">
</div>
